package com.miot.restapi.util;


import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.ApiModel;

import java.util.List;

/**
 * @Author: zhangyanbin
 * @Date: 2019/1/8 17:50
 * @Version 1.0
 */
@ApiModel(description= "返回响应数据")
public class DataResult extends JSONObject {
    private static final long serialVersionUID = 1L;

    public DataResult() {
        put("code", "0");
    }

    public static DataResult error() {
        return error("-100", "0000：主人，程序开小差了，请联系客服");
    }

    public static DataResult error(String msg) {
        return error("-100", msg);
    }

    public static DataResult error(String code, String msg) {
        DataResult r = new DataResult();
        r.put("code", code);
        r.put("information", msg);
        r.put("success", false);
        return r;
    }

    public static DataResult ok(String msg) {
        DataResult r = new DataResult();
        r.put("information", msg);
        r.put("success", true);
        return r;
    }

    public static DataResult info(Boolean flag, String successMsg, String errorMsg) {
        if (flag) {
            return ok(successMsg);
        } else {
            return error(errorMsg);
        }
    }

    public static DataResult ok(Object object) {
        DataResult r = new DataResult();
        r.put("information", "成功！");
        r.put("success", true);
        r.put("data", object);
        return r;
    }

    public static DataResult ok() {
        DataResult r = new DataResult();
        r.put("information", "成功！");
        r.put("success", true);
        return r;
    }

    /**
     * @param data
     * @param totalNum  总数量
     * @param pageSize  每页数量
     * @param totalPage 总页数
     * @param pageNum   当前页
     * @param success   true
     * @param code      0
     * @return
     */
    public static DataResult page(List<?> data, long totalNum, int pageSize, int pageNum, int totalPage, Boolean success, int code) {
        DataResult r = new DataResult();
        r.clear();
        r.put("data", data);
        r.put("totalNum", totalNum);
        r.put("pageSize", pageSize);
        r.put("pageNum", pageNum);
        r.put("totalPage", totalPage);
        r.put("success", success);
        r.put("code", code);
        return r;
    }

    @Override
    public DataResult put(String key, Object value) {
        super.put(key, value);
        return this;
    }
}
